<?php session_start(); ?>
<?php

class HomeController extends Controller
{
	/*public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations
		);
	}*/
	public function allowedActions()
	{
		return '*';
	}
	
	public function actionIndex($alias = null, $search = null)
	{
		$this->pageTitle = $this->arrSystem[0]->title;
		
		// lay du lieu cac  theme
		$arrPrice = Prices::model()->getDataPrice2();
		
		// lay du lieu model tin tuc
		$criteriaNew = new CDbCriteria;
		$criteriaNew->addCondition("categories.display=1");
		$criteriaNew->with = array('categories');
		$criteriaNew->order = 't.id DESC';
		$arrNew = News::model()->findAll($criteriaNew);
		
		// lay du lieu model danh muc tin tuc
		$criteriaCategoryNew = new CDbCriteria;
		$criteriaCategoryNew->addCondition("display=1");
		$criteriaCategoryNew->limit = 2;
		$arrCategories = CategoriesNews::model()->findAll($criteriaCategoryNew);
		
		// lay du lieu model danh muc theme
		$arrCateTheme = CategoriesWebsite::model()->findAll();
		
		// lay du lieu model theme
		$criteria = new CDbCriteria;
		if($alias != null && $search != ''){
			$cri = new CDbCriteria;
			$cri->addCondition("alias LIKE '{$alias}'");
			$id = CategoriesWebsite::model()->find($cri);
			$id = $id->id;
			$criteria->addCondition("id_categoiresWebsite={$id} and name LIKE '{$search}%' ");
		}else{
			if($alias != null){
				$cri = new CDbCriteria;
				$cri->addCondition("alias LIKE '{$alias}'");
				$id = CategoriesWebsite::model()->find($cri);
				$id = $id->id;
				$criteria->addCondition("id_categoiresWebsite={$id}");
			}
			if($search != null) $criteria->addCondition("name LIKE '{$search}%' ");
		}
		$criteria->order = 'id DESC';
		$criteria->limit = 16;
		$arrTheme = Websites::model()->findAll($criteria);
		
		$modelRegisterTheme = new RegisterTheme;

		$this->render("index", array(
			'arrNew' => $arrNew,
			'arrCategories' => $arrCategories,
			'arrCateTheme' => $arrCateTheme,
			'arrTheme' => $arrTheme,
			'modelRegisterTheme' => $modelRegisterTheme,
			'arrPrice' => $arrPrice,
		));
	}
	
	public function actionStringToImage(){
		header ("Content-type: image/png");
		$string = Yii::app()->session['captcha'] = random_string('alnum', 4);                                         
		$font   = 6;
		$width  = ImageFontWidth($font) * strlen($string);
		$height = ImageFontHeight($font);

		$im = @imagecreate ($width,$height);
		$background_color = imagecolorallocate ($im, 255, 255, 255); //white background
		$text_color = imagecolorallocate ($im, 0, 0,0);//black text
		imagestring ($im, $font, 0, 0,  $string, $text_color);
		imagepng ($im);
	}
	
	public function actionRegisterTheme(){
		if($_POST['captcha'] == Yii::app()->session['captcha']){
			$model = new RegisterTheme;
			$model->name = $_POST['name'];
			$model->phone = $_POST['phone'];
			$model->email = $_POST['email'];
			$model->id_price = $_POST['id_price'];
			$model->created = time();
			$model->save();
			echo "true";
		}else{
			echo "false";
		}
	}
	
	public function actionPrice()
	{
		$this->pageTitle = "Bảng giá - ".$this->arrSystem[0]->title;
			
		$this->render("prices", array(
		));
	}
	
	public function actionTheme($alias = null, $name = null, $sort = null)
	{
		$this->pageTitle = "Siêu thị website - ".$this->arrSystem[0]->title;
		
		// lay du lieu model danh muc theme
		$arrCateTheme = CategoriesWebsite::model()->findAll();
		
		// lay du lieu model theme
		$criteria = new CDbCriteria;
		
		if($alias != null){
			$cri = new CDbCriteria;
			$cri->addCondition("alias LIKE '{$alias}'");
			$id = CategoriesWebsite::model()->find($cri);
			$id = $id->id;
			$cate = CategoriesWebsite::model()->findByPK($id);
			$title = $cate->name;
		}else $title = "";
		
		if($alias != null && $name != ''){
			$cri = new CDbCriteria;
			$cri->addCondition("alias LIKE '{$alias}'");
			$id = CategoriesWebsite::model()->find($cri);
			$id = $id->id;
			$criteria->addCondition("id_categoiresWebsite={$id} and name LIKE '%{$name}%' ");
		}else{
			if($alias != null){
				$criteria->addCondition("id_categoiresWebsite={$id}");
				$cri = new CDbCriteria;
				$cri->addCondition("alias LIKE '{$alias}'");
				$id = CategoriesWebsite::model()->find($cri);
				$id = $id->id;
			}
			if($name != null) $criteria->addCondition("name LIKE '%{$name}%' ");
		}
		
		if($sort != null) $criteria->order = $sort.' DESC';
		else $criteria->order = 'created DESC';
		
		$countItem =  Websites::model()->count($criteria);
                
        $pages = new CPagination($countItem);
        $pages->setPageSize(9);
        $pages->applyLimit($criteria);  // the trick is here!

		$arrTheme = Websites::model()->findAll($criteria);
		$numberTheme =  Websites::model()->count();
		
		// lay cac danh muc theme duoc xem nhieu nhat
		$criteria = new CDbCriteria;
		$criteria->order = 'numberView DESC';
		$criteria->limit = 4;
		$arrCate = CategoriesWebsite::model()->findAll($criteria);
		
		$this->render("theme", array(
			"arrCateTheme" => $arrCateTheme,
			"arrTheme" => $arrTheme,
			"numberTheme" => $numberTheme,
			'item_count'=>$countItem,
			'page_size'=>9,
			'pages'=>$pages,
			'title' => $title,
			'arrCate' => $arrCate,
		));
	}
	
	public function actionThemeDetail($alias = null){
	
		// lay theme theo alias
		$cri = new CDbCriteria;
		$cri->addCondition("alias LIKE '{$alias}'");
		$theme = Websites::model()->find($cri);
		$cate = null;
		if(!empty($theme)){
			$cate = CategoriesWebsite::model()->findByPK($theme->id_categoiresWebsite);
			if($cate !== null)
			{
				$cate->numberView = $cate->numberView+1;
				$cate->save();
			}
		}
		
		// lay cac theme cung loai
		$criteria = new CDbCriteria;
		$criteria->addCondition("alias NOT LIKE '{$alias}' and id_categoiresWebsite={$theme->id_categoiresWebsite}");
		$criteria->order = 'id DESC';
		$criteria->limit = 2;
		
		$themeRelated =  Websites::model()->findAll($criteria);
		
		$this->pageTitle = $theme->name." - ".$this->arrSystem[0]->title;
		$this->render("themeDetail", array(
			'theme' => $theme,
			'cate' => $cate,
			'themeRelated' => $themeRelated,
		));
	}
	
	public function actionImageDetail($alias = null){
	
		$cri = new CDbCriteria;
		$cri->addCondition("alias LIKE '{$alias}'");
		$theme = Websites::model()->find($cri);
		$criteria = new CDbCriteria;
		$criteria->addCondition("id_website={$theme->id}");
		$arrImageTheme = ImagesWebsite::model()->findAll($criteria);
		
		$this->pageTitle = "Demo theme ".$theme->name." - ".$this->arrSystem[0]->title;
		$this->render("imageDetail", array("theme" => $theme,'arrImageTheme'=>$arrImageTheme));
	}
	
	public function actionViewDemo($alias = null){
	
		$cri = new CDbCriteria;
		$cri->addCondition("alias LIKE '{$alias}'");
		$theme = Websites::model()->find($cri);

		$this->pageTitle = "Demo theme ".$theme->name." - ".$this->arrSystem[0]->title;
		$this->render("viewDemo", array("theme" => $theme));
	}

	public function actionNewDetail($alias = null){
	
		$criteria = new CDbCriteria;
		$criteria->addCondition("alias LIKE '{$alias}' ");
		$new = News::model()->find($criteria);
		$cateNew = null;
		if(!empty($new)){
			$cateNew =  CategoriesNews::model()->findByPK($new->category_news_id);
			$cri = new CDbCriteria;
			$cri->addCondition("category_news_id = $new->category_news_id and alias NOT LIKE '{$alias}'");
			$cri->limit =  2;
			$newRelated = News::model()->findAll($cri);
		}
		
		$this->pageTitle = $new->name." - ".$this->arrSystem[0]->title;;
		$this->render("newDetail", array(
			'new'=>$new,
			'cateNew' => $cateNew,
			'newRelated' => $newRelated,
		));
	}
	
	public function actionListNews($alias = null){
	
		$this->pageTitle = "Tin tức - ".$this->arrSystem[0]->title;
		
		$criteria = new CDbCriteria;
		$criteria->addCondition("alias LIKE '{$alias}' ");
		$cateNew = CategoriesNews::model()->find($criteria);
		
		$criteria1 = new CDbCriteria;
		if(!empty($cateNew)){
			$criteria1->addCondition("category_news_id = {$cateNew->id} ");
		}
		
		$countItem =  News::model()->count($criteria1);
                
        $pages = new CPagination($countItem);
        $pages->setPageSize(4);
        $pages->applyLimit($criteria1);  // the trick is here!
		
		$arrNew = News::model()->findAll($criteria1);

		$this->render("listNews", array(
			'arrNew' => $arrNew,
			'cateNew' => $cateNew,
			'item_count'=>$countItem,
			'page_size'=>4,
			'pages'=>$pages,
		));
	}
	
	public function actionListService(){
	
		$this->pageTitle = "Dịch vụ - ".$this->arrSystem[0]->title;
		
		$arrService = Services::model()->findAll();
		$this->render("listService", array('arrService' =>$arrService));
	}
	
	public function actionServiceDetail($alias = null){
	
		$criteria = new CDbCriteria;
		$criteria->addCondition("alias LIKE '{$alias}'");
		$service = Services::model()->find($criteria);
		$this->pageTitle = $service->name." - ".$this->arrSystem[0]->title;;
		$this->render("serviceDetail", array('service' =>$service));
	}
	
	public function actionRemoveCateSearch($alias = null){
		$criteria = new CDbCriteria;
		$criteria->addCondition("alias LIKE '{$alias}'");
		$cate = CategoriesWebsite::model()->find($criteria);
		$cate->numberView = 0;
		$cate->save();
		echo true;
	}
	
	public function actionSearchDomain(){
		$this->render("searchDomain");
	}
	
	
	
}